window.eventBus = new Vue();

import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
import VueRouter from 'vue-router'
import VueResource from 'vue-resource'
import { Loading } from 'element-ui';

// 引入css样式文件
import './assets/css/style.css';
import './assets/fonts/iconfont.css';

// 引入vue-amap
import AMap from 'vue-amap';

//引入基础路由和项目路由
import EipRouter from './router/eip-router.js'

import API from './api/base_api.js';
global.API = API;


Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(VueResource)
Vue.use(AMap)

Vue.prototype.setCookie = function (c_name, value, expiredays) {
    var exdate = new Date()
    exdate.setDate(exdate.getDate() + expiredays)
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
}

Vue.prototype.getCookie = function (c_name) {
    if (document.cookie.length > 0) {
        var c_start = document.cookie.indexOf(c_name + "=")
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1
            var c_end = document.cookie.indexOf(";", c_start)
            if (c_end == -1) c_end = document.cookie.length
            return unescape(document.cookie.substring(c_start, c_end))
        }
    }
    return ""
}

// 初始化vue-amap
AMap.initAMapApiLoader({
  // 高德的key
  key: '1caf7e2255d40bceac95775754061197',
  // 插件集合
  plugin: ['AMap.Autocomplete', 'AMap.Geocoder', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor']
});

Vue.http.interceptors.push(function (request, next) {
    //post提交数据的时候，给限制一下下一次的提交间隔
    var loadingInstance;
    if(request.method == "POST") {
        var div = document.getElementById("tab-content-div");
        console.log(div);
        loadingInstance = Loading.service({ fullscreen: true});
        // console.log(request);
        // var lastUrl = localStorage.lastUrl;
        // if(lastUrl == request.url) {
        //     var lastTime = localStorage.lastTime;
        //     var currTime = new Date().getTime();

        //     if((currTime - lastTime) < 1000) {
        //         console.log("操作太快了");
        //         localStorage.lastUrl = request.url;
        //         localStorage.lastTime = new Date().getTime();

        //         return;
        //     }
        // }

        // localStorage.lastUrl = request.url;
        // localStorage.lastTime = new Date().getTime();
    }

    if(Vue.prototype.getCookie("token") == null || Vue.prototype.getCookie("token") == "") {
        this.$router.push({ path: '/login' });
    }
    // request.headers.set('Authorization', sessionStorage.Authorization);
    //重新把cookie设置生效时间
    Vue.prototype.setCookie("token", Vue.prototype.getCookie("token"), 1/24);
    request.headers.set('Authorization', Vue.prototype.getCookie("token"));

    next(function (response) {
        if (loadingInstance != null) { 
            loadingInstance.close();
        }
        
        console.log("回来的数据是" + response.body.status);
        if (response.body.status == -104) {
            this.$router.push({ path: '/login' });
        }
    });
});


const routes = EipRouter;

/**
 * 新建一个路由，引入路由表中的地址
 */
const router = new VueRouter({
    base: '/admin/',
    routes
})

new Vue({
  el: '#app',
  router: router
})
